package oop.ex2.commands;

import oop.ex2.exceptions.UnrecognizedVariable;
import oop.ex2.expression.ExpressionSolver;

/**
 * This is the base class for all of the assignment commands that apply an operation
 * to an existing variable. They all require the given variable to exist. Each subclass implements
 * a different operation that's applyed to the expression before assignment.
 * @author daniel_i_l
 *
 */
public abstract class AssignmentOpCommand extends AssignmentCommand {

	/**
	 * Build an assignment operation command using a given expression solver and assignment
	 * operation string.
	 * @param expressionSolver The expression solver to use.
	 * @param assignmentOp The assignment operation string to look for.
	 */
	public AssignmentOpCommand(ExpressionSolver expressionSolver, String assignmentOp) {
		super(expressionSolver, assignmentOp);
	}

	@Override
	protected void assignValue(String varName, double expVal) throws UnrecognizedVariable {
		if (!_expressionSolver.hasVariable(varName))
			throw new UnrecognizedVariable("the assignment variable isn't recognozed");
		
		double assignVal = preassignmentOperation(_expressionSolver.getVarValue(varName), expVal);
		_expressionSolver.setVarValue(varName, assignVal);

	}
	
	/**
	 * Takes the values of the variable and the expression
	 * and performs some preassignment operation. 
	 * @param varVar The variable value.
	 * @param expVal The expressionValue.
	 * @return
	 */
	public abstract double preassignmentOperation(double varVal, double expVal); 

}
